﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace UWP.Douban.Api
{
    public static class Common
    {

        #region UriBuilder

        internal const string SCHEME = "https";
        internal const string HOST = "api.douban.com";
        internal const string DOUBAN = "www.douban.com";
        internal const int PORT = 443;

        #endregion

        internal const string API_KEY = "00a0951fbec80b0501e1bf5f3c58210f";
        internal const string REDIRECT_URI = "http://group.douban.com/!service/android";
        internal const string GRANT_TYPE = "password";
        internal const string CLIENT_SECRET = "77faec137e9bda16";
        internal const string UDID = "db855c32d3bc99277024dc9b45d23398";

        #region Web Request Headers

        public static string Accept = "*/*";
        public static string AcceptEncoding = "gzip";
        public static string ContentType = "application/x-www-form-urlencoded;charset=utf-8";
        public static string UserAgent = "NativeHost";
        public static string Host = "www.douban.com";
        public static string Connection = "Keep-Alive";
        public static string CacheControl = "no-cache";
        public static string Authorization = "Authorization";
        public static string ContentLength = "ContentLength";

        public static string AuthHeader => string.Format("Bear {0}", Token);

        #endregion

        #region Token

        public static string Token = "";
        public static string UserId = "";
        public static string UserName { get; set; }
        public static string ExpiresIn { get; set; }
        public static string RefreshToken { get; set; }
        public static DateTime TokenTime { get; set; }


        public static Error LastError { get; internal set; }
     
        #endregion

        public static bool TokenIsReady()
        {
            return !string.IsNullOrEmpty(Token) && DateTime.Now.Subtract(TokenTime).TotalSeconds < double.Parse(ExpiresIn);
        }
    }

    public class Error
    {
        private WebException e;

        public Error(WebException e)
        {
            this.e = e;
        }
    }
}
